<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品录入ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	create by allen.qiang
 */
quanli_require('module/base/goods/goodsAdd.class.php');
class ajax_goodsAdd {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {
				case 1: //根据商品基本信息组获取到基本信息值
					$this->getBasic();
				break;
				case 2: //商品原材料信息
					$this->getMater();
				break;
				case 3: //商品主信息添加or编辑
					$this->addGoods();
				break;
				case 4: //商品sku添加or编辑
					$this->addGoodsSku();
				break;
				case 5: //商品生产信息添加or编辑
					$this->addGoodsProduct();
				break;
				case 6:	//查询当前三级商品并展示在页面
					$this->searchSaleShow();
				break;
				case 7:	//保存sku成本信息
					$this->saveCost();	
				break;
				case 8:	//保存生产信息
					$this->saveProduct();	
				break;
				case 9:	//查询已选‘生产信息’和‘营养成分’
					$this->searchSelProduct();	
				break;
				case 10: //保存成本信息
					$this->saveCostInfo();	
				break;
				case 11: //保存图片信息
					$this->savePicInfo();	
				break;
				default:
					returnJsonData('参数错误');
				break;	
            }
        }
    }

	/**
	 * 根据商品基本信息组获取到基本信息值
	 *
	 */
	public function getBasic(){
		
		global $G_SHOP;
		
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
    	$result_array = $addHandler->getBasicTpl( $_REQUEST ) ;
    	if ($result_array['code'] == 200) {
	    	$G_SHOP->smarty->assign_by_ref('basic_result', $result_array['data']);
	    	$basicTr = $G_SHOP->smarty->fetch('goods/basicfetch.tpl');
	    	echo json_encode(array('code' => 200, 'data' => $basicTr, 'sort' => $result_array['sort']));
    	} else {
    		echo json_encode($result_array);
    	}

	}
	
	/**
	 * 商品原材料信息
	 *
	 */
	public function getMater(){
		
		global $G_SHOP;
		
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
    	$result_array = $addHandler->getMaterTpl( $_REQUEST ) ;
    	if ($result_array['code'] == 200) {
	    	$G_SHOP->smarty->assign_by_ref('mater_result', $result_array['data']);
	    	$materTr = $G_SHOP->smarty->fetch('goods/materfetch.tpl');
	    	echo json_encode(array('code' => 200, 'data' => $materTr, 'sort' => $result_array['sort']));
    	} else {
    		echo json_encode($result_array);
    	}
	}
	
	/**
	 * 商品主信息添加or编辑
	 * @Explain  添加和编辑通过postID进行区分，
	 *           编辑商品主信息不能修改商品的类目、
	 *           所属城市、名称
	 *
	 */
	public function addGoods(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->addGoods( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 商品sku添加or编辑
	 *
	 */
	public function addGoodsSku(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->addGoodsSku( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 查询当前三级商品并展示在页面
	 *
	 */
	public function searchSaleShow(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->searchSaleShow( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 保存sku成本信息
	 *
	 */
	public function saveCost(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->saveCost( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 保存生产信息
	 *
	 */
	public function saveProduct(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->saveProduct( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 查询已选‘生产信息’和‘营养成分’
	 *
	 */
	public function searchSelProduct(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->searchSelProduct( $_REQUEST ) ;

    	if ($result_array['code'] == 200) {
    		if (!empty($result_array['data']['product_result'])) {
	    		$G_SHOP->smarty->assign_by_ref('product_result', $result_array['data']['product_result']);
		    	$productTr = $G_SHOP->smarty->fetch('goods/productfetch.tpl');
    		}
	    	
    		if (!empty($result_array['data']['nutrition_result'])) {
		    	$G_SHOP->smarty->assign_by_ref('nutri_result', $result_array['data']['nutrition_result']);
		    	$nutriTr = $G_SHOP->smarty->fetch('goods/nutrifetch.tpl');
    		}
	    	
	    	$result_array['data']['productTr'] = isset($productTr) ? $productTr : '';
	    	$result_array['data']['nutriTr'] = isset($nutriTr) ? $nutriTr : '';

	    	echo json_encode($result_array);
    	} else {
    		echo json_encode($result_array);
    	}
	}
	
	
	public function saveCostInfo(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->saveCostInfo( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 保存图片信息
	 *
	 */
	public function savePicInfo(){
		global $G_SHOP;
		$addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $addHandler->savePicInfo( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
}